package _04_tx;

import org.apache.rocketmq.client.producer.LocalTransactionState;
import org.apache.rocketmq.client.producer.TransactionListener;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.concurrent.TimeUnit;

public class MyTransactionListener implements TransactionListener {

    @Override
    public LocalTransactionState executeLocalTransaction(Message msg, Object arg) {
        //执行本地事务
        System.out.println("==============执行本地事务================");
        System.out.println(msg);
        System.out.println("arg: "+arg); // 我们自己传递的参数

        try {
            TimeUnit.SECONDS.sleep(120);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("==============本地事务执行完成================");
//        // 返回本地事务执行状态
//       COMMIT_MESSAGE, 已提交
//       ROLLBACK_MESSAGE, 回滚
//      UNKNOW, 未知
        return LocalTransactionState.UNKNOW;
    }

    @Override
    public LocalTransactionState checkLocalTransaction(MessageExt msg) {
        //检查本地事务
        System.out.println("==============检查本地事务================");
        System.out.println(msg);
        System.out.println("msgID: "+msg.getMsgId()); // 根据消息ID 查询事务执行状态

        return LocalTransactionState.UNKNOW;
    }
}
